<?php
/**
 * Created by PhpStorm.
 * Service: Administrator
 * Date: 2018/6/6/006
 * Time: 9:32
 */

namespace App\Http\Controllers\Demo;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Service\User\UserService;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\Redis;
use Session,Config;
use Cache;

class IndexController extends Controller
{
    /**
     * @return view
     * Demo页面
     */
    public function demo()
    {
        return view('Demo.account');
    }

    /**
     * @param Request $request
     * 添加账号
     */
    public function addAccount(Request $request)
    {

        $where = $request->except('_token');

        $user = new UserService();

        $ret =  $user->addAccount($where);

        if($ret['error_code'] == '0')
        {
            echo '<script>location.href="/demo";alert("账号添加成功！");</script>';
        }else{
            echo "<script>confirm('{$ret['error_message']}')?location.href='/demo' : history.back() ;</script>";
            exit();
        }

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     * 删除账号
     */
    public function delAccount(Request $request)
    {

        $where = [];
        $where['account_id']=trim($request->input('account_id'),',');


        $user = new UserService();
        $data = $user->deleteAccount($where);

        if($request->ajax())
        {
            return response()->json($data);
        }else{

            if($data['error_code'] == '0')
            {
                echo '<script>location.href="/demo";alert("删除成功！");</script>';
            }else{
                echo "<script>confirm('{$data['error_message']}')?location.href='/demo' : history.back() ;</script>";
                exit();
            }
        }

    }

    /**
     * @param Request $request
     * 修改账户信息
     */
    public function updateAccount(Request $request)
    {
        $where = [];
        $where= $request->except('_token');
        $reg = "/[[:punct:]]/i";
        $where['remark'] = preg_replace($reg, '', $where['remark']);

        $user = new UserService();
        $data = $user->updateAccount($where);

        if($data['error_code'] == '0')
        {
            echo '<script>location.href="/demo";alert("账号信息修改成功！");</script>';
        }else{
            echo "<script>confirm('{$data['error_message']}')?location.href='/demo' : history.back() ;</script>";
            exit();
        }
    }

    /**
     * @param Request $request
     * 用户账号列表
     */
    public function getAccountList(Request $request)
    {

        $where= $request->except('_token');

        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }

        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $user = new UserService();
        $data = $user->getAccountList($where);


        foreach ($data['data']['data'] as &$v){
            if($v['daily_limit_type'] == 1){
                $v['daily_limit_type_name'] = '不限量';
            }else{
                $v['daily_limit_type_name'] = '限量';
            }
        }


        return json_encode($data);

    }





}